import 'package:kq_flutter_core_widget/config/kq_core_global.dart';
import 'package:kq_flutter_widgets/config/kq_global.dart';
import 'package:kq_flutter_widgets/theme/configs/common/kq_common_theme_config.dart';
import 'package:kq_flutter_widgets/theme/configs/dialog/kq_dialog_theme_config.dart';
import 'package:kq_flutter_widgets/theme/configs/form/kq_form_theme_config.dart';
import 'package:kq_flutter_widgets/theme/configs/pullDown/kq_pullDown_theme_config.dart';

import 'configs/button/kq_button_theme_config.dart';
import 'configs/kq_theme_config.dart';
import 'default/kq_default_config_utils.dart';

/// 主题配置管理类
class KqThemeManager {
  KqThemeManager._() {
    _checkAndInitKqConfig();
  }

  static final KqThemeManager _instance = KqThemeManager._();

  static KqThemeManager get instance {
    return _instance;
  }

  /// 主题配置Map，用于多个主题切换
  Map<String, KqThemeConfig> globalConfig = <String, KqThemeConfig>{};

  /// 检查是否有默认配置
  bool isKqThemeConfig() => globalConfig[kqThemeConfigId] != null;

  void _checkAndInitKqConfig() {
    if (!isKqThemeConfig()) {
      globalConfig[kqThemeConfigId] = KqDefaultConfigUtils.defaultAllConfig;
    }
  }

  /// 注册主题配置，不传configId，则修改默认主题，多个主题可以用多个configId。
  void register(KqThemeConfig? allThemeConfig,
      {String configId = kqThemeConfigId}) {
    if (allThemeConfig != null) {
      globalConfig[configId] = allThemeConfig;
    }
  }

  /// 获取当前的主题的配置
  KqThemeConfig getConfig() {
    KqThemeConfig? allThemeConfig =
        globalConfig[KqGlobal.themeConfigId] ?? globalConfig[kqThemeConfigId];
    assert(allThemeConfig != null, 'No suitable config found.');
    return allThemeConfig!;
  }

  /// 获取当前主题的通用配置
  static KqCommonThemeConfig getCommonConfig() {
    return KqThemeManager.instance.getConfig().commonConfig;
  }

  /// 获取当前主题的按钮配置
  static KqButtonThemeConfig getButtonConfig() {
    return KqThemeManager.instance.getConfig().buttonThemeConfig;
  }

  /// 获取当前主题的表单配置
  static KqFormThemeConfig getFormConfig() {
    return KqThemeManager.instance.getConfig().formConfig;
  }

  /// 获取当前主题的对话框配置
  static KqDialogThemeConfig getDialogConfig() {
    return KqThemeManager.instance.getConfig().dialogConfig;
  }

  /// 获取当前主题的下拉菜单配置
  static KqPullDownMenuThemeConfig getPullDownMenuConfig() {
    return KqThemeManager.instance.getConfig().pullDownMenuThemeConfig;
  }
}
